<?php

namespace App\Store\Controllers\Basic;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Store\Models\Basic\GoodsPricetype;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Validate\Basic\GoodsPricetype as ValidateBasicGoodsPricetype;

/**
 * 价格类型信息
 */
class GoodsPricetypeController extends Controller
{
    /**
     * 获取列表
     */
    public function getlist(Request $request)
    {
        $list = GoodsPricetype::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 获取详情
     */
    public function getinfo(Request $request)
    {
        $id = $request->id ?? 0;
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $info = GoodsPricetype::where(['id' => $id, 'cp_id' => $cp_id, 'is_del' => 1])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['info' => $info]]);
    }

    /**
     * 新增
     */
    public function dopostadd(Request $request)
    {
        $req_data = $request->only(['pricetype_name','remark']);

        $Validate = new ValidateBasicGoodsPricetype();
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }
        $accountinfo= StoreCommon::getCurrentAccountinfo($request);
        $isexist=GoodsPricetype::where([['pricetype_name', $req_data['pricetype_name']], ['cp_id',$accountinfo['cp_id']]])->first();
        if($isexist){
            return response()->json(['code' => '001', 'msg' => '价格类型不允许重复']);
        }
        $data = $req_data;
        $data['create_account_id']=$accountinfo['id'];
        $data['update_account_id']=$accountinfo['id'];
        $data['create_account_real_name'] = $accountinfo['real_name'];
        $data['update_account_real_name'] = $accountinfo['real_name'];
        $data['cp_id'] = $accountinfo['cp_id'];
        $data['create_at'] = time();
        $data['update_at'] = time();
        GoodsPricetype::create($data);
        return response()->json(['code' => '000', 'msg' => '新增成功']);
    }

    /**
     * 编辑
     */
    public function dopostedit(Request $request)
    {
        $req_data = $request->only(['id', 'pricetype_name','remark']);

        $Validate = new ValidateBasicGoodsPricetype('edit');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }
        $accountinfo= StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];
        
        $info = GoodsPricetype::where([['id', $req_data['id']], ['cp_id', $cp_id], ['is_del', 1]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        $isexist=GoodsPricetype::where([['id','<>',$req_data['id']],['pricetype_name', $req_data['pricetype_name']], ['cp_id',$accountinfo['cp_id']]])->first();
        if($isexist){
            return response()->json(['code' => '001', 'msg' => '价格类型不允许重复']);
        }

        $data = $req_data;
        unset($data['id']);
        $data['update_at'] = time();
        $data['update_account_id']=$accountinfo['id'];
        $data['update_account_real_name'] = $accountinfo['real_name'];
        GoodsPricetype::where(['id' => $req_data['id']])->update($data);
        return response()->json(['code' => '000', 'msg' => '编辑成功']);
    }
    /**
     * 删除
     */
    public function dodel(Request $request)
    {
        $id = $request->id ?? 0;
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $info = GoodsPricetype::where([['id', $id], ['cp_id', $cp_id], ['is_del', 1]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        GoodsPricetype::where([['id', $id]])->update(['is_del' => 2, 'del_at' => time()]);

        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

}
